package com.hundsun.documentripper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hundsun.documentripper.domain.dto.NodeTemplateView;
import com.hundsun.documentripper.domain.pojo.NodeTemplate;
import com.hundsun.documentripper.domain.pojo.NodeTemplateField;
import com.hundsun.documentripper.mapper.NodeTemplateFieldMapper;
import com.hundsun.documentripper.mapper.NodeTemplateMapper;
import com.hundsun.documentripper.service.NodeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：节点模板接口实现
 * @date ：Created in 2021/1/28
 */
@Service
public class NodeTemplateServiceImpl implements NodeTemplateService {

    @Autowired
    private NodeTemplateMapper nodeTemplateMapper;
    @Autowired
    private NodeTemplateFieldMapper nodeTemplateFieldMapper;

    @Override
    public NodeTemplateView getNodeTemplateView(String nodeTemplateUuid) {
        if (!StringUtils.isEmpty(nodeTemplateUuid)) {
            NodeTemplateView nodeTemplateView = NodeTemplateView.fromNodeTemplate(nodeTemplateMapper.selectById(nodeTemplateUuid));
            // 根据模板编号获取模板域
            QueryWrapper<NodeTemplateField> wrapper = new QueryWrapper<>();
            wrapper.eq("node_template_uuid", nodeTemplateUuid);
            nodeTemplateView.setNodeTemplateFields(nodeTemplateFieldMapper.selectList(wrapper));
            return nodeTemplateView;
        } else {
            return null;
        }
    }

    @Override
    // @Cacheable(cacheNames = {"nodeTemplateView"})
    public List<NodeTemplateView> getNodeTemplateView(List<String> nodeTemplateUuidList) {
        if (nodeTemplateUuidList.isEmpty()) {
            return new ArrayList<>();
        }
        // 获取模板列表
        QueryWrapper<NodeTemplate> nodeTemplateWrapper = new QueryWrapper<>();
        nodeTemplateWrapper.in("uuid", nodeTemplateUuidList);
        List<NodeTemplate> nodeTemplateList = nodeTemplateMapper.selectList(nodeTemplateWrapper);
        // 获取模板域列表
        QueryWrapper<NodeTemplateField> nodeTemplateFiledWrapper = new QueryWrapper<>();
        nodeTemplateFiledWrapper.in("node_template_uuid", nodeTemplateUuidList);
        List<NodeTemplateField> nodeTemplateFieldList = nodeTemplateFieldMapper.selectList(nodeTemplateFiledWrapper);
        // 模板域根据模板编号分组
        Map<String, List<NodeTemplateField>> nodeTemplateFieldMap = nodeTemplateFieldList.stream()
                .collect(Collectors.groupingBy(NodeTemplateField::getNodeTemplateUuid));
        // 构造成展示对象
        List<NodeTemplateView> nodeTemplateViewList = new ArrayList<>();
        for (NodeTemplate nodeTemplate : nodeTemplateList) {
            NodeTemplateView nodeTemplateView = NodeTemplateView.fromNodeTemplate(nodeTemplate);
            nodeTemplateView.setNodeTemplateFields(nodeTemplateFieldMap.get(nodeTemplate.getUuid()));
            nodeTemplateViewList.add(nodeTemplateView);
        }
        return nodeTemplateViewList;
    }

    @Override
    public void saveNodeTemplateViewList(List<NodeTemplateView> nodeTemplateViewList) {
        if (null != nodeTemplateViewList) {
            for (NodeTemplateView nodeTemplateView : nodeTemplateViewList) {
                nodeTemplateMapper.insert(nodeTemplateView);
                for (NodeTemplateField nodeTemplateField : nodeTemplateView.getNodeTemplateFields()) {
                    nodeTemplateField.setUuid(UUID.randomUUID().toString());
                    nodeTemplateField.setNodeTemplateUuid(nodeTemplateView.getUuid());
                }
                nodeTemplateFieldMapper.insertBatchSomeColumn(nodeTemplateView.getNodeTemplateFields());
            }
        }
    }

}
